package xin.marcher.module.replenished.mq.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xin.marcher.module.common.constants.RocketMqConstant;
import xin.marcher.module.replenished.mq.config.RocketMQProperties;
import xin.marcher.module.replenished.mq.consumer.listener.WarningInventoryListener;


/**
 * 供需功能所需的消息消费
 *
 *
 */
@Configuration
public class ConsumerBeanConfig {

    /**
     * 配置内容对象
     */
    @Autowired
    private RocketMQProperties rocketMQProperties;


    /**
     * 库存预警的消息
     *
     * @param warningInventoryListener
     * @return
     * @throws MQClientException
     */
    @Bean("warningInventoryTopic")
    public DefaultMQPushConsumer productUpdateTopic(WarningInventoryListener warningInventoryListener) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(RocketMqConstant.WARNING_INVENTORY_GROUP);
        consumer.setNamesrvAddr(rocketMQProperties.getNameServer());
        consumer.subscribe(RocketMqConstant.WARNING_INVENTORY_TOPIC, "*");
        consumer.registerMessageListener(warningInventoryListener);
        consumer.start();
        return consumer;
    }
}
